package utils;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import component.ShowDailogComponent;
import constants.Constants;

import java.awt.event.ActionEvent;
import java.io.*;
import java.net.URLDecoder;
import java.util.Properties;

/**
 * @Auther syc.
 * @Date 2016/11/10 13:56.
 */
public class GameUtils {

    public static String getPropertyValue(String pluginPath, String key) {
        Properties prop = getGameProperties(pluginPath, Constants.PROPERTY_FILE);
        return prop.getProperty(key);
    }

    public static String getPropertyValue(AnActionEvent anActionEvent, String key) {
        String pluginPath = getPluginPath(anActionEvent);
        return getPropertyValue(pluginPath, key);
    }

    private static String getPluginPath(AnActionEvent anActionEvent) {
        String pluginPath = anActionEvent.getProject().getBasePath() + File.separator + Constants.PLUGIN_DIR;
        return pluginPath;
    }

    public static Properties getGameProperties(String pluginPath, String fileName) {

        FileInputStream fis = null;
        Properties prop = new Properties();

        try {
            String filePath = getFilePath(pluginPath, fileName);
//            if(!isFileExist(filePath)) log("工程路径配置有误，请检查后重新配置, path = " + filePath);
            fis = new FileInputStream(filePath);
            prop.load(fis);
//log("get file prop: " + prop);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return prop;
    }

    public static void setGameProperties(AnActionEvent anActionEvent,String key, String value, String comment) {
        String pluginPath = getPluginPath(anActionEvent);
        setGameProperties(pluginPath, key, value, comment);
    }

    public static void setGameProperties(String pluginPath, String key, String value, String comment) {
        FileOutputStream fos = null;
        Properties prop = getGameProperties(pluginPath, Constants.PROPERTY_FILE);

        if (prop == null) {
            System.out.println("No found the file of game.properties!");
            return;
        }
        if (value == null) {
            value = "";
        }
        prop.setProperty(key, value);

        try {
            String filePath = getFilePath(pluginPath, Constants.PROPERTY_FILE);
//log("set file : " + filePath);
            fos = new FileOutputStream(filePath);
            prop.store(fos, comment);
//log("set file prop: " + prop);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String getFilePath(String pluginPath, String fileName) {

        String relativePath = pluginPath + File.separator + fileName;
//        String relativePath = "plugin" + File.separator + fileName;

        File file = new File(relativePath);
        File parent = file.getParentFile();

        if (parent != null && !parent.exists()) {
            parent.mkdirs();
        }
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String filePath = file.getPath();

        //处理中文路径
        filePath = filterPathOfEncoding(filePath, "UTF-8");

        return filePath;
    }

    public static void log(String message) {
        Application application = ApplicationManager.getApplication();
        ShowDailogComponent component = application.getComponent(ShowDailogComponent.class);
        component.showDialog(message);
    }

    public static String createBatFileWithPathLimited(AnActionEvent anActionEvent,  String path, String batFilePrex, String... commands) {
        String pluginPath = getPluginPath(anActionEvent);
        return createBatFileWithPathLimited(pluginPath, path, batFilePrex, commands);
    }

    public static String createBatFileWithPathLimited(String pluginPath, String path, String batFilePrex, String... commands) {
        String filePath = getFilePath(pluginPath, batFilePrex.replace(" ", "_") + "YT.bat");
        BufferedWriter out = null;
        // 确认文件分区
        String part = path.split(":")[0];
        // 进入cmd命令生效目录所在磁盘区
        String cdCommand1 = part + ":";
        String cdCommand2 = "cd " + path;
        String[] cmds = new String[commands.length + 2];
        cmds[0] = cdCommand1;
        cmds[1] = cdCommand2;

        for (int i = 2; i < commands.length + 2; i++) {
            cmds[i] = commands[i - 2];
        }
        createBatFile(pluginPath, batFilePrex, cmds);
        return filePath;
    }

    public static String createBatFile(AnActionEvent anActionEvent, String batFilePrex, String... commands) {
        String pluginPath = getPluginPath(anActionEvent);
        return createBatFile(pluginPath, batFilePrex, commands);
    }
    public static String createBatFile(String pluginPath, String batFilePrex, String... commands) {
        String filePath = getFilePath(pluginPath, batFilePrex.replace(" ", "_") + "YT.bat");
        BufferedWriter out = null;
        try {

            //处理中文路径
            filePath = filterPathOfEncoding(filePath, "UTF-8");

            FileWriter fw = new FileWriter(filePath);
//            Writer fw = new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8");

            out = new BufferedWriter(fw);

            String cmdInBat = "";
            for (String command : commands) {
                cmdInBat = cmdInBat + command + "\n";
            }

            String bat = "@echo off\n" +
                    "title start\n" +
                    "\n" +
                    cmdInBat +
                    "\n" +
                    "pause";
            out.write(bat);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return filePath;
    }

    public static String filterPathOfEncoding(String filePath, String encoding) {
        try {
            filePath = URLDecoder.decode(filePath, encoding);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return filePath;
    }

    public static boolean isFileExist(String path) {
        if (path == null) {
            log("路径配置存在问题，请重新配置合法路径, path = " + path);
            return false;
        }

        File file = new File(path);
        if (!file.exists()) {
            log("路径配置存在问题，请重新配置合法路径, path = " + path);
            return false;
        }
        return true;
    }
}
